package com.aplink.generic;

import java.io.IOException;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;

import com.aplink.generic.controller.GenericApplication;
import com.aplink.generic.database.DatabaseHelper;
import com.aplink.generic.pojo.CloudNotification;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.util.LogUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public abstract class GenericSplashScreenActivity extends Activity {
    protected class PrefetchData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(final Void... params) {
            GenericSplashScreenActivity.this.mMasterConfig = GenericApplication
                    .getInstance().getMasterConfig();
            if (GenericSharePreference.getInstance().isFirstStart()) {
                if (GenericSplashScreenActivity.this.mMasterConfig != null) {
                    // Create Database;
                    DatabaseHelper.getInstance();
                    // Set configurations

                    GenericSharePreference.getInstance().setOfflineMode(
                            GenericSplashScreenActivity.this.mMasterConfig
                                    .isHasOfflineMode());
                    if (GenericSplashScreenActivity.this.mMasterConfig
                            .getAutoSync() != null) {
                        GenericSharePreference.getInstance().setEnableAutoSync(
                                GenericSplashScreenActivity.this.mMasterConfig
                                        .getAutoSync().isEnable());
                    }
                    if (GenericSplashScreenActivity.this.mMasterConfig
                            .getCloudNotification() != null) {
                        GenericSharePreference.getInstance()
                                .setHasCloudMessage(true);
                    }

                    // Register device id with google cloud message.
                    if (GenericSharePreference.getInstance().hasCloudMessage()) {
                        GenericSplashScreenActivity.this.mCloudMessaging = GoogleCloudMessaging
                                .getInstance(GenericSplashScreenActivity.this);
                        String registrationID = GenericSharePreference
                                .getInstance().getRegistrationId();
                        if (TextUtils.isEmpty(registrationID)) {
                            final CloudNotification cloudNotification = GenericSplashScreenActivity.this.mMasterConfig
                                    .getCloudNotification();
                            try {
                                registrationID = GenericSplashScreenActivity.this.mCloudMessaging
                                        .register(cloudNotification.getSender());
                                if (!TextUtils.isEmpty(registrationID)) {
                                    LogUtil.info(
                                            "Device registered, registration ID="
                                                    + registrationID,
                                            "RegisterCloudMessage", getClass()
                                                    .getSimpleName());
                                    GenericSharePreference.getInstance()
                                            .setRegistrationId(registrationID);
                                }
                            } catch (final IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(final Void result) {
            super.onPostExecute(result);
            if (!TextUtils
                    .isEmpty(GenericSplashScreenActivity.this.mMasterConfig
                            .getLoginClass())) {
                try {
                    final ActivityInfo activityInfo = getActivityInfo(GenericSplashScreenActivity.this.mMasterConfig
                            .getLoginClass());
                    final Class<?> activityClass = Class
                            .forName(activityInfo.name);
                    final Intent intent = new Intent(
                            GenericSplashScreenActivity.this, activityClass);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    overridePendingTransition(android.R.anim.slide_in_left,
                            android.R.anim.slide_out_right);
                    GenericSplashScreenActivity.this.startActivity(intent);
                    finish();
                } catch (final ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (final NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private GoogleCloudMessaging mCloudMessaging;
    private MasterConfig mMasterConfig;

    private ActivityInfo getActivityInfo(final String activityName)
            throws NameNotFoundException {
        final String packageName = GenericApplication.getInstance()
                .getApplicationContext().getPackageName();
        final ActivityInfo[] listActivity = GenericApplication.getInstance()
                .getApplicationContext().getPackageManager()
                .getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).activities;
        for (final ActivityInfo activityInfo : listActivity) {
            if (activityInfo.name.toLowerCase(Locale.getDefault()).contains(
                    activityName.toLowerCase(Locale.getDefault()))) {
                return activityInfo;
            }
        }
        return null;
    }

    public abstract int getLayout();

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(getLayout());
    }

    @Override
    protected void onResume() {
        super.onResume();
        new PrefetchData().execute();
    }
}
